var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var app = getApp();

Page({
  data: {
    bannerInfo: {
      'imgUrl': '',
      'name': ''
    },
    categoryFilter: false,
    filterCategory: [],
    goodsList: [],
    categoryId: 0,
    currentSortType: 'default',
    currentSortOrder: 'desc',
    page: 1,
    size: 6,
    totalPages: 1,
    remindValue: ['', '', '送'],
    plusFlag: ['', '+', '  '],
    memberGoodsNum: [],
    integralGoodsNum: []
  },

  getBanner: function() {
    let that = this;
    util.request(api.GoodsNew).then(function(res) {
      if (res.errno === 0) {
        that.setData({
          bannerInfo: res.data.bannerInfo,
          memberGoodsNum: app.globalData.memberGoodsNum,
          integralGoodsNum: app.globalData.integralGoodsNum
        });
      }
    });
  },

  getGoodsList: function() {
    wx.showLoading({
      title: '加载中...',
    });
    var that = this;
    util.request(api.GoodsList, {
        isNew: 1,
        page: that.data.page,
        size: that.data.size,
        order: that.data.currentSortOrder,
        sort: that.data.currentSortType,
        categoryId: that.data.categoryId
      })
      .then(function(res) {
        if (res.errno === 0) {
          that.setData({
            goodsList: that.data.goodsList.concat(res.data.goodsList),
            filterCategory: res.data.filterCategoryList,
            totalPages: Math.ceil(res.data.count / 6)
          });
        }
        wx.hideLoading();
      });
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.getBanner();
    this.getGoodsList();
  },
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {
    // 页面显示

  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  onHide: function() {
    // 页面隐藏

  },
  onUnload: function() {
    // 页面关闭

  },
  onReachBottom() {
    if (this.data.page < this.data.totalPages) {
      setTimeout(() => {
        this.setData({
          page: this.data.page + 1
        });
        this.getGoodsList()
      }, 500)
    } else {
      wx.showToast({
        title: '暂无更多数据',
        icon: "none"
      })
      this.setData({
        loadingText: "已无更多数据"
      })
    }
  },

  openSortFilter: function(event) {
    let currentId = event.currentTarget.id;
    switch (currentId) {
      case 'categoryFilter':
        this.setData({
          categoryFilter: !this.data.categoryFilter,
          currentSortType: 'category',
          currentSortOrder: 'asc'
        });
        break;
      case 'priceSort':
        let tmpSortOrder = 'asc';
        if (this.data.currentSortOrder == 'asc') {
          tmpSortOrder = 'desc';
        }
        this.setData({
          currentSortType: 'price',
          currentSortOrder: tmpSortOrder,
          categoryFilter: false
        });

        this.getGoodsList();
        break;
      default:
        //综合排序
        this.setData({
          currentSortType: 'default',
          currentSortOrder: 'desc',
          categoryFilter: false,
          categoryId: 0
        });
        this.getGoodsList();
    }
  },
  selectCategory: function(event) {
    let currentIndex = event.target.dataset.categoryIndex;
    this.setData({
      goodsList: [],
      categoryFilter: false,
      categoryId: this.data.filterCategory[currentIndex].id
    });
    this.getGoodsList();
  }
})